#pragma once
#include <vector>
#include <assert.h>

namespace bit
{
    template<size_t N>
    class bitset
    {
    public:

        bitset()
        {
            _bits.resize( N / 32 + 1 , 0);
        }

        //把x映射的位标记成1
        void set(size_t x)
        {
            assert(x <= N);
            size_t i = x / 32;
            size_t j = x % 32;

            _bits[i] |= (1 << j);
        }

        //把x映射的位标记成0
        void reset(size_t x)
        {
            assert(x <= N);
            size_t i = x / 32;
            size_t j = x % 32;

            _bits[i] &= ~(1 << j);
        }

        bool test(size_t x)
        {
            assert(x <= N);
            size_t i = x / 32;
            size_t j = x % 32;

            return _bits[i] & (1 << j);
        }

    private:
        std::vector<int> _bits;
    };


    template<size_t N>
    class two_bit_set
    {
    public:
        void set(size_t x)
        {
            //00 -> 01
            if(_bs1.test(x) == false && _bs2.test(x) == false)
            {
                _bs2.set(x);
            }
            //01->10
            else if(_bs1.test(x) == false && _bs2.test(x) == true)
            {
                _bs1.set(x);
                _bs2.reset(x);
            }
            //10->11
            else if(_bs1.test(x) == true && _bs2.test(x) == false)
            {
                _bs1.set(x);
                _bs2.set(x);
            }
        }

        int test()
        {
            if(_bs1.test(x) == false && _bs2.test(x) == false)
            {
                return 0;
            }
            //01->10
            else if(_bs1.test(x) == false && _bs2.test(x) == true)
            {
                return 1;
            }
            else
            {
                return 2;
            }
        }

    private:
        bitset<N> _bs1;
        bitset<N> _bs2;
    };

}


